/*******************************************************************************
Replication Materials for Blau, Kahn, Brummund, Cook, and Larson-Koester "Is 
There Still Son Preference in the United States?"

Table A1

See "CPS Read Me" for variable definitions

Date Modified: 10/17/2019

*******************************************************************************/

*Define needed variables
local v "core_samp core_sampf secgen genrace native citizen marst female head_samp"
local v "`v' nchild18 frever year gt0chld age c_pemomtyp* c_foster* hhwtnorm"

*Load Data
use `v' using "$cps\regfile_June_core", clear

		
*Drop widows, natives born abroad, men
drop if marst==5
drop if citizen==1
drop if female==0

*Keep only women who are the household head or spouse of the household head_samp, have at least one child
keep if head_samp==1
keep if gt0chld==1

*Keep only years in which the live births variable is available
keep if inlist(year, 2008,2010,2012)==1

*Generate variables to flag families with adopted or step children
egen stepadopt = rowmax(c_pemomtyp*)
	la def step 1 "all biological" 2 "at least one step" 3 "at least one adopted"
	la val stepadopt step
	
egen foster = rowmax(c_foster*) //Note these families are dropped in the core sample

/*******************************************************************************

I. Define Variable that Checks Sample Against Live Births

*******************************************************************************/
	
replace frever = . if frever == 999
drop if frever == .

gen great = .
replace great = 0 if nchild18 	== frever & frever != . 
replace great = 1 if nchild18 	< frever & frever != . 
replace great = 2 if nchild18 	> frever & frever != . 

la def great 0 "sample=live" 1 "sample less than live" 2 "sample greater than live"
la val great great
	
	
/*******************************************************************************

II. Make Table Content

*******************************************************************************/	

mat drop _all

mat TableA1 = J(24, 6, .)

*Loop through demographic samples
forv jj = 0/5 {

		if (`jj' == 0) {
			local ifst ""
			local top = 1
		}
		else if (`jj' == 1) {
			local ifst "& native == 1"
			local top = 5
		}
		else if (`jj' == 2) {
			local ifst "& native == 0"
			local top = 9
		}
		else if (`jj' == 3) {
			local ifst "& genrace == 3 & native == 0"
			local top = 13
		}
		else if (`jj' == 4) {
			local ifst "& genrace == 2 & native == 0"
			local top = 17
		}
		else if (`jj' == 5) {
			local ifst "& secgen == 1"
			local top = 21
		}
	
	*** All Women, Ages 18-40
	local r = `top'
	tab great if inrange(age,18,40)==1 `ifst' [aweight = hhwtnorm], matcell(freq)
	local left = 1
	foreach val in 1 2 3 {
		local c = `left'
		mat TableA1[`r', `c'] = freq[`val',1]
		local c = `c'+1
		mat TableA1[`r', `c'] = (freq[`val',1] / (freq[1,1] + freq[2,1] + freq[3,1]))*100
		local r = `r' + 1
		}
	
	*** Core Fertility Sample (Married)
	local r = `top'
	tab great if stepadopt == 1 & foster==0 & core_sampf==1 `ifst' [aweight = hhwtnorm], matcell(freq)
	local left = 3
	foreach val in 1 2 3 {
		local c = `left'
		mat TableA1[`r', `c'] = freq[`val',1]
		local c = `c'+1
		mat TableA1[`r', `c'] = (freq[`val',1] / (freq[1,1] + freq[2,1] + freq[3,1]))*100
		local r = `r' + 1
		}
	
	*** Core Headship Sample (Head or Spouse of the Head)
	local r = `top'
	tab great if stepadopt == 1 & foster==0 & core_samp==1 `ifst' [aweight = hhwtnorm], matcell(freq)
	local left = 5
	foreach val in 1 2 3 {
		local c = `left'
		mat TableA1[`r', `c'] = freq[`val',1]
		local c = `c'+1
		mat TableA1[`r', `c'] = (freq[`val',1] / (freq[1,1] + freq[2,1] + freq[3,1]))*100
		local r = `r' + 1
		}

		}

putexcel set "$toutput\Table_A1.xlsx", replace
putexcel B6=matrix(TableA1), nformat("#####")


/*******************************************************************************

III. Make Table Labels

*******************************************************************************/

putexcel A1="Table A1: Children in Sample Compared to Reported Live Births"

putexcel A5="A. All Groups"
putexcel A9="B. Natives"
putexcel A13="C. Immigrants"
putexcel A17="D. Asian Immigrants"
putexcel A21="E. Hispanic Immigrants"
putexcel A25="F. Second Generation Immigrants"

forval r=6(4)26 {
	putexcel A`r'="Sample=Live"
}

forval r=7(4)27 {
	putexcel A`r'="Sample Less Than Live"
}

forval r=8(4)28 {
	putexcel A`r'="Sample Greater Than Live"
}

putexcel B2 = "All"
putexcel C2 = "All"
putexcel D2 = "Restricted"
putexcel E2 = "Restricted"
putexcel F2 = "Restricted"
putexcel G2 = "Restricted"

putexcel B3 = ""
putexcel C3 = ""
putexcel D3 = "Married"
putexcel E3 = "Married"
putexcel F3 = "All"
putexcel G3 = "All"

putexcel B4 = "Count"
putexcel C4 = "Percent"
putexcel D4 = "Count"
putexcel E4 = "Percent"
putexcel F4 = "Count"
putexcel G4 = "Percent"